﻿
using QDasTransfer.Classes;
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace QDasTransfer.Forms
{
    public partial class ConfigForm : Form
    {
        public ParamaterData pd = new ParamaterData();
        public bool needSave = false;

        public ConfigForm()
        {
            InitializeComponent();
        }

        private void setFolder(TextBox tb)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.SelectedPath = tb.Text;
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tb.Text = fbd.SelectedPath;
            }
        }

        private void ConfigForm_Load(object sender, EventArgs e)
        {
            // this.Text += "  (当前配置文件: " + new FileInfo(QTrans.TransferBase.appconfig).FullName + ")";
            if (values.transducer?.ConfigPage != null)
            {
                TabPage page = new TabPage(values.transducer.CompanyName);
                page.Controls.Add(values.transducer.ConfigPage);
                this.tabControl1.TabPages.Add(page);
                values.transducer.LoadConfig();
            }

            // pgOutput
            txtOutputFolder.Text = pd.OutputFolder;
            txtTempFolder.Text = pd.TempFolder;
            if (pd.ChangeFileNameTypeIfExisted == 0)
                rbReplaceOld.Checked = true;
            else if (pd.ChangeFileNameTypeIfExisted == 1)
                rbAddTimeToNew.Checked = true;
            else if (pd.ChangeFileNameTypeIfExisted == 2)
                rbIncrementIndex.Checked = true;

            if (pd.KeepOutFolderStructLevel > 0)
            {
                ckKeepOutFolderStruct.Checked = true;
                numlblKeepFolderLevel.Value = pd.KeepOutFolderStructLevel;
                numlblKeepFolderLevel.Enabled = true;
            }
            else
            {
                ckKeepOutFolderStruct.Checked = false;
            }

            ckAddTimeTickToOutDFQfile.Checked = pd.AddTimeTickToOutputFile;
            ckKeepOutFolderStruct.Checked = pd.KeepOutFolderStructType == 1;

            //pgBackups 
            rbBackup.Checked = pd.ProcessSourceFileType == 0;
            rbNoChange.Checked = pd.ProcessSourceFileType == 1;
            rbDelete.Checked = pd.ProcessSourceFileType == 2;
            gbBackupFolders.Enabled = rbBackup.Checked;
            txtSuccessfulFolder.Text = pd.SucceededFolder;
            txtFailedFolder.Text = pd.FailedFolder; 

            //pgAutoTransducer
            cbCircleUnit.SelectedIndex = 1;
            if (pd.SupportAutoTransducer)
            { 
                gbAutoConfig.Enabled = pd.AutoTransducerAvaliable;
                numCircleValue.Value = pd.AutomaticInterval;
                dpStartTime.Value = pd.StartTime;
                dpEndTime.Value = pd.EndTime;
            }


            /***** encoding page *****/
            lbEncodings.Items.Clear();
            foreach (Encoding ed in ParamaterData.Encodings)
                lbEncodings.Items.Add(ed.BodyName);
            lbEncodings.Items[0] = "default";
        }

        private void btOutputFolder_Click(object sender, EventArgs e)
        {
            setFolder(txtOutputFolder);
        }

        private void btnTempFolder_Click(object sender, EventArgs e)
        {
            setFolder(txtTempFolder);
        }

        private void btnSuccessfulFolder_Click(object sender, EventArgs e)
        {
            setFolder(txtSuccessfulFolder);
        }

        private void btnFailedFolder_Click(object sender, EventArgs e)
        {
            setFolder(txtFailedFolder);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            needSave = true;

            // pgOutput
            pd.OutputFolder = txtOutputFolder.Text;
            pd.TempFolder = txtTempFolder.Text;
            if (rbReplaceOld.Checked)
                pd.ChangeFileNameTypeIfExisted = 0;
            else if (rbAddTimeToNew.Checked)
                pd.ChangeFileNameTypeIfExisted = 1;
            else
                pd.ChangeFileNameTypeIfExisted = 2;
            pd.KeepOutFolderStructType = ckKeepOutFolderStruct.Checked ? 1 : 0;
            pd.AddTimeTickToOutputFile = ckAddTimeTickToOutDFQfile.Checked;

            //pgBackups
            if (rbBackup.Checked)
                pd.ProcessSourceFileType = 0;
            else if (rbNoChange.Checked)
                pd.ProcessSourceFileType = 1;
            else if (rbDelete.Checked)
                pd.ProcessSourceFileType = 2;
			 
            pd.SucceededFolder = txtSuccessfulFolder.Text;
            pd.FailedFolder = txtFailedFolder.Text;

            //pgAutoTransducer 
            if (pd.AutoTransducerAvaliable)
            {
                pd.AutomaticInterval = (int)numCircleValue.Value; 
                pd.StartTime = dpStartTime.Value;
                pd.EndTime = dpEndTime.Value;
            }
			 

            /****** Encoding page ******/  
            values.transducer.SaveConfig();

            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ckKeepFolderStruct_Click(object sender, EventArgs e)
        {
            numlblKeepFolderLevel.Enabled = ckKeepOutFolderStruct.Checked;
        }

        private void ckAutoTransfer_Click(object sender, EventArgs e)
        {
 
        }

        private void rbBackup_CheckedChanged(object sender, EventArgs e)
        {
            gbBackupFolders.Enabled = true;
        }

        private void rbNoChange_CheckedChanged(object sender, EventArgs e)
        {
            gbBackupFolders.Enabled = false;
        }

        private void rbDelete_CheckedChanged(object sender, EventArgs e)
        {
            gbBackupFolders.Enabled = false;
        }

		private void tiAddFile_Click(object sender, EventArgs e)
		{

		}

		private void tiAddFolder_Click(object sender, EventArgs e)
		{

		}

		private void tiAddPathManually_Click(object sender, EventArgs e)
		{

		}

		private void tiSelectAll_Click(object sender, EventArgs e)
		{

		}

		private void tiCancelAll_Click(object sender, EventArgs e)
		{

		}

		private void tiSelectReverse_Click(object sender, EventArgs e)
		{

		}

		private void tiDeletePathes_Click(object sender, EventArgs e)
		{

		}
	}
}
